<template>
  <div class="dropdown" ref="dropdownRef">
    <a class="btn btn-secondary dropdown-toggle" @click.prevent="toggleOpen">
      {{ title }}</a
    >

    <ul class="dropdown-menu" style="display: block" v-if="isOpen">
      <slot></slot>
    </ul>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch } from 'vue'
import { useClickOutSide } from '../hooks/useClickOutSide'
export default defineComponent({
  name: 'DropDown',
  props: {
    title: {
      type: String,
      required: true
    }
  },
  setup() {
    const dropdownRef = ref<null | HTMLElement>(null)
    const isOpen = ref(false)
    const toggleOpen = () => {
      isOpen.value = !isOpen.value
    }
    const isClickOut = useClickOutSide(dropdownRef)

    watch(isClickOut, () => {
      if (isOpen.value && isClickOut.value) {
        isOpen.value = false
      }
    })

    return { dropdownRef, isOpen, toggleOpen }
  }
})
</script>

<style lang="" scoped></style>
